<?php

namespace app\admin\controller;

use app\common\util\CosUploads;
use app\common\util\ErrorCode;
use app\common\util\Uploads;
use app\common\model\trResource;

class UploadController extends CommonController
{
    public $upload = '';

    public function initialize()
    {
        parent::initialize();
        if(request()->server("IS_SCF")) {
            $this->upload = new CosUploads();
        }else{
            $this->upload = new Uploads();
        }
    }

    /**
     * @return mixed
     * 图片上传方法
     */
    public function uploadImage()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        //上传图片
        $upload_config = config('upload.uploadConf');
        $config = array(
            "ext" => $upload_config['imageAllowFilesTP'],
            "size" => $upload_config['imageMaxSize'],
        );
        if(isset($this->params["sizeRule"])){
            $config["sizeWH"] = explode(",", $this->params["sizeRule"]);
        }
        if(isset($this->params["extRule"])){
            $config["ext"] = explode(",", $this->params["extRule"]);
        }
        $fieldName = $upload_config['imageFieldName'];
        $result = $this->upload->upImage($fieldName, $config, $this->getSessionIdVar($this->params['token'], 'id'));
        if(isset($result['msg'])){
            return $this->return_result(ErrorCode::$errorCode['customized'], $result['msg'] ?: "上传图片失败");
        }
        $this->returnData['data'] = $result;
        return $this->return_result($this->returnData);
    }

    /**
     * @return mixed
     * 文件上传方法
     */
    public function upFile()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $this->upload->savePath = "file/";
        //上传文件
        $upload_config = config('upload.uploadConf');
        $config = array(
            "ext" => $upload_config['fileAllowFilesTP'],
            "size" => $upload_config['fileMaxSize'],
        );
        $fieldName = $upload_config['fileFieldName'];
        $result = $this->upload->upFile($fieldName, $config, $this->getSessionIdVar($this->params['token'], 'id'));
        if($result['state'] == "SUCCESS"){
            $this->returnData["data"] = $result;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = $result["msg"];
        }
        return $this->return_result($this->returnData);
    }

    /**
     * @return mixed
     * ueditor编辑器上传方法
     */
    public function ueditor()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }

        //编辑器上传
        $this->upload->savePath = 'ueditor/';
        $adminId = $this->getSessionIdVar(input('token'), "id");
        $result = $this->upload->ueditor($adminId);
        echo json($result)->send();
        exit;
    }

    public function getAll()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }

        $page = input('page') ? input('page') : 1;
        $limit = input('pageSize') ? input('pageSize') : config('paginate.list_rows');
        $imageList = trResource::getListByPaginate(["location" => "admin", "is_delete" => 0, "type" => 1, "mall_id" => request()->mallData["id"]], $page, $limit);
        if(!$imageList['data']){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            foreach ($imageList['data'] as $k => $v) {
                $imageList['data'][$k]['url'] = tome_dia($v["url"]);
            }
            //echo "<pre>";print_r($imageList);die("2");
            $this->returnData['data'] = ['list' => $imageList['data'], 'totalPage' => $imageList['last_page'], 'currentPage' => $imageList['current_page'], 'count' => $imageList["total"]];
        }
        return $this->return_result($this->returnData);
    }

    //软删除
    public function softDelete()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }

        $image_id = input('ids', null, 'htmlspecialchars');
        if(empty($image_id)){
            return $this->return_result(ErrorCode::$errorCode['params_not_exist'], "ids");
        }
        $image_id = explode(",", $image_id);
        trResource::delSoftById($image_id);
        return $this->return_result($this->returnData);
    }

    public function delete()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }

        $image_id = input('ids', null, 'htmlspecialchars');
        if(empty($image_id)){
            return $this->return_result(ErrorCode::$errorCode['params_not_exist'], "ids");
        }
        $image_id = explode(",", $image_id);
        trResource::delById($image_id);
        return $this->return_result($this->returnData);
    }
}
